package cn.sdhuijin.vms.controller;

import java.text.ParseException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.sdhuijin.vms.controller.comand.PageInfo;
import cn.sdhuijin.vms.model.CarRecord;
import cn.sdhuijin.vms.model.PersonRecord;
import cn.sdhuijin.vms.service.IRecordService;
import cn.sdhuijin.vms.sms.parser.Util;

@Controller
@RequestMapping("/record")
public class RecordController {

	@Resource(name = "myRecordService")
	private IRecordService recordService;

	@RequestMapping(value = "/person", method = RequestMethod.GET)
	public String show() {
		return "personRecord/show";
	}

	@RequestMapping(value = "/person/list", method = RequestMethod.GET)
	public String list(PageInfo pageInfo, String startTime, String endTime,
			String personName, String orgId, Model model) {
		Page<PersonRecord> page = null;
		try {
			page = recordService.getPersonRecordListByDateAndName(
					pageInfo.getPage(), pageInfo.getRows(),
					Util.fromString(startTime), Util.fromString(endTime),
					personName, orgId);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		model.addAttribute("page", page);
		return "personRecord/list";
	}

	@RequestMapping(value = "/person/export", method = RequestMethod.POST)
	public void export(String startTime, String endTime, String personName,
			String orgId, Model model, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			List<PersonRecord> list = recordService
					.getPersonRecordListByDateAndName(
							Util.fromString(startTime),
							Util.fromString(endTime), personName, orgId);
			if (list != null) {
				Util.exportPersonRecordExcelDocument(list, request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "/car", method = RequestMethod.GET)
	public String show2() {
		return "carRecord/show";
	}

	@RequestMapping(value = "/car/export", method = RequestMethod.POST)
	public void exportCarRecord(String beginTime, String finalTime,
			String carLicense, String orgId, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			List<CarRecord> list = recordService
					.getCarRecordListByDateAndLicense(
							Util.fromString(beginTime),
							Util.fromString(finalTime), carLicense, orgId);
			if (list != null) {
				Util.exportCarRecordExcelDocument(list, request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "/car/list", method = RequestMethod.GET)
	public String list2(PageInfo pageInfo, String beginTime, String finalTime,
			String carLicense, String orgId, Model model) {
		Page<CarRecord> page = null;
		try {
			page = recordService.getCarRecordListByDateAndLicense(
					pageInfo.getPage(), pageInfo.getRows(),
					Util.fromString(beginTime), Util.fromString(finalTime),
					carLicense, orgId);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		model.addAttribute("page", page);
		return "carRecord/list";
	}
}
